package cn.jh.clearing.service;

import java.math.BigDecimal;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import cn.jh.clearing.business.PaymentOrderBusiness;
import cn.jh.clearing.business.ProfitRecordBusiness;
import cn.jh.clearing.pojo.BrandWithdrawRebate;
import cn.jh.clearing.pojo.PaymentOrder;
import cn.jh.clearing.pojo.ProfitRecord;
import cn.jh.clearing.util.Util;
import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.RandomUtils;

@EnableScheduling
@Configuration
public class BrandScheduleService {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PaymentOrderBusiness paymentOrderBusiness;

	@Autowired
	Util util;

	@Autowired
	private ProfitRecordBusiness profitRecordBusiness;

	@Value("${schedule-task.on-off}")
	private String scheduleTaskOnOff;

	/**
	 * 周结算提现费 周一 2:10
	 */
	@Scheduled(cron = "0 10 2 ? * MON")
	public void weekWithdrawClearing() {
		if ("true".equals(scheduleTaskOnOff)) {

			logger.info("周提现分润结算");

			Date todayDate = new Date();

			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");

			Calendar date = Calendar.getInstance();
			date.setTime(todayDate);
			date.set(Calendar.DATE, date.get(Calendar.DATE) - 7);

			Date endDate = null;
			try {
				endDate = dft.parse(dft.format(date.getTime()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//提现返利
			List<PaymentOrder> brandWithdrawRebates = paymentOrderBusiness.queryWeekBrandWithdrawRebate(endDate,todayDate);
			
			if(brandWithdrawRebates!=null)
			for (PaymentOrder withdrawRebate : brandWithdrawRebates) {
				/*** 获取平台的管理员账号 */
				long brandid = withdrawRebate.getBrandid();
				String ordercode = withdrawRebate.getOrdercode();
				RestTemplate restTemplate = new RestTemplate();
				URI uri = util.getServiceUrl("user", "error url request!");
				String url = uri.toString() + "/v1.0/user/brand/query/id?brand_id=" + brandid;
				String result = restTemplate.getForObject(url, String.class);
				/** 根据的渠道标识或去渠道的相关信息 */
				logger.info("RESULT================" + result);
				JSONObject jsonObject = JSONObject.fromObject(result);
				if(!jsonObject.getString("resp_code").equals(CommonConstants.SUCCESS)){
					logger.info("失败返利订单================" + withdrawRebate.getOrdercode()+"============brand_id==="+brandid);
					continue;
				}
				JSONObject resultObj = 	jsonObject.getJSONObject("result");
				String manageid = resultObj.getString("manageid");

				/** 根据manageid获取用户 */
				uri = util.getServiceUrl("user", "error url request!");
				url = uri.toString() + "/v1.0/user/query/id";
				/** 根据的渠道标识或去渠道的相关信息 */
				MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
				requestEntity = new LinkedMultiValueMap<String, String>();
				requestEntity.add("id", manageid);
				result = restTemplate.postForObject(url, requestEntity, String.class);
				logger.info("RESULT================" + result);
				jsonObject = JSONObject.fromObject(result);
				if(!jsonObject.getString("resp_code").equals(CommonConstants.SUCCESS)){
					logger.info("失败返利订单================" + withdrawRebate.getOrdercode()+"============manageid==="+manageid);
					continue;
				}
				resultObj = jsonObject.getJSONObject("result");
				String phone = resultObj.getString("phone");

				/** 根据订单号获取订单 */
				PaymentOrder paymentOrder = paymentOrderBusiness.queryPaymentOrderBycode(ordercode);

				ProfitRecord profitRecord = profitRecordBusiness.queryProfitRecordByordercode(ordercode, "1");
				if (profitRecord != null) {
					continue;
				} else {
					/** 存贮分润记录明细 */
					profitRecord = new ProfitRecord();
					profitRecord.setId(Long.parseLong(RandomUtils.generateNumString(8)));
					profitRecord.setAcqAmount(new BigDecimal("1"));
					profitRecord.setAcqphone(phone);
					profitRecord.setAcquserid(Long.parseLong(manageid));
					profitRecord.setAmount(paymentOrder.getAmount());
					profitRecord.setCreateTime(new Date());
					profitRecord.setOrdercode(ordercode);
					profitRecord.setOriphone(paymentOrder.getPhone());
					profitRecord.setOrirate(paymentOrder.getRate());
					profitRecord.setOriuserid(paymentOrder.getUserid());
					profitRecord.setRemark("品牌提现分润");
					profitRecord.setType("1");
					profitRecord.setScale(BigDecimal.ONE);
					profitRecordBusiness.merge(profitRecord);

					/** 存储 用户的分润记录 */
					restTemplate = new RestTemplate();
					uri = util.getServiceUrl("user", "error url request!");
					url = uri.toString() + "/v1.0/user/rebate/update";
					/** 根据的渠道标识或去渠道的相关信息 */
					requestEntity = new LinkedMultiValueMap<String, String>();
					requestEntity.add("rebate_amount", "1");
					requestEntity.add("user_id", manageid);
					requestEntity.add("order_code", ordercode);
					result = restTemplate.postForObject(url, requestEntity, String.class);
				}
			}
			//自清充值返利
			List<PaymentOrder> brandWithdrawClearRebates = paymentOrderBusiness.queryWeekBrandWithdrawClearRebate(endDate,todayDate);
			if(brandWithdrawClearRebates!=null)
			for (PaymentOrder withdrawRebate : brandWithdrawClearRebates) {

				/*** 获取平台的管理员账号 */
				long brandid = withdrawRebate.getBrandid();
				String ordercode = withdrawRebate.getOrdercode();
				RestTemplate restTemplate = new RestTemplate();
				URI uri = util.getServiceUrl("user", "error url request!");
				String url = uri.toString() + "/v1.0/user/brand/query/id?brand_id=" + brandid;
				String result = restTemplate.getForObject(url, String.class);
				/** 根据的渠道标识或去渠道的相关信息 */
				logger.info("RESULT================" + result);
				JSONObject jsonObject = JSONObject.fromObject(result);
				if(!jsonObject.getString("resp_code").equals(CommonConstants.SUCCESS)){
					logger.info("失败返利订单================" + withdrawRebate.getOrdercode()+"============brand_id==="+brandid);
					continue;
				}
				JSONObject resultObj = jsonObject.getJSONObject("result");
				String manageid = resultObj.getString("manageid");

				/** 根据manageid获取用户 */
				uri = util.getServiceUrl("user", "error url request!");
				url = uri.toString() + "/v1.0/user/query/id";
				/** 根据的渠道标识或去渠道的相关信息 */
				MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
				requestEntity = new LinkedMultiValueMap<String, String>();
				requestEntity.add("id", manageid);
				result = restTemplate.postForObject(url, requestEntity, String.class);
				logger.info("RESULT================" + result);
				jsonObject = JSONObject.fromObject(result);
				if(!jsonObject.getString("resp_code").equals(CommonConstants.SUCCESS)){
					logger.info("失败返利订单================" + withdrawRebate.getOrdercode()+"============manageid==="+manageid);
					continue;
				}
				resultObj = jsonObject.getJSONObject("result");
				String phone = resultObj.getString("phone");

				/** 根据订单号获取订单 */
				PaymentOrder paymentOrder = paymentOrderBusiness.queryPaymentOrderBycode(ordercode);

				ProfitRecord profitRecord = profitRecordBusiness.queryProfitRecordByordercode(ordercode, "1");
				if (profitRecord != null) {
					continue;
				} else {
					BigDecimal AcqAmount=new BigDecimal("0.00").setScale(2, BigDecimal.ROUND_DOWN) ;
					try {
						AcqAmount=paymentOrder.getExtraFee().subtract(paymentOrder.getCostfee()).setScale(2, BigDecimal.ROUND_DOWN);
					} catch (Exception e) {
						logger.info("RESULT=============getExtraFee===" + paymentOrder.getExtraFee()+"====================getCostfee==="+paymentOrder.getCostfee());
					}
					/** 存贮分润记录明细 */
					profitRecord = new ProfitRecord();
					profitRecord.setId(Long.parseLong(RandomUtils.generateNumString(8)));
					profitRecord.setAcqAmount(AcqAmount);
					profitRecord.setAcqphone(phone);
					profitRecord.setAcquserid(Long.parseLong(manageid));
					profitRecord.setAmount(paymentOrder.getAmount());
					profitRecord.setCreateTime(new Date());
					profitRecord.setOrdercode(ordercode);
					profitRecord.setOriphone(paymentOrder.getPhone());
					profitRecord.setOrirate(paymentOrder.getRate());
					profitRecord.setOriuserid(paymentOrder.getUserid());
					profitRecord.setRemark("品牌自清分润");
					profitRecord.setType("1");
					profitRecord.setScale(BigDecimal.ONE);
					profitRecordBusiness.merge(profitRecord);

					/** 存储 用户的分润记录 */ 
					restTemplate = new RestTemplate();
					uri = util.getServiceUrl("user", "error url request!");
					url = uri.toString() + "/v1.0/user/rebate/update";
					/** 根据的渠道标识或去渠道的相关信息 */
					requestEntity = new LinkedMultiValueMap<String, String>();
					requestEntity.add("rebate_amount", AcqAmount.toString());
					requestEntity.add("user_id", manageid);
					requestEntity.add("order_code", ordercode);
					result = restTemplate.postForObject(url, requestEntity, String.class);
				}
			}
		}
	}

}
